package com.shenchuangit.webprintclient.print;

import javax.print.*;
import javax.print.attribute.PrintRequestAttributeSet;
import java.io.*;

public class PicturePrintTask extends PrintTask {
    /**
     * 打印格式，默认autosense
     */
    private DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

    public PicturePrintTask() {
    }

    public PicturePrintTask(DocFlavor docFlavor) {
        this.docFlavor = docFlavor;
    }

    @Override
    public void print(File fileToPrint, PrintRequestAttributeSet aset) {
        // 构造待打印的文件流
        InputStream fis = null;
        try {
            fis = new FileInputStream(fileToPrint);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Doc doc = new SimpleDoc(fis, this.docFlavor, null);
        // 创建打印作业
        DocPrintJob job = this.getPrintService().createPrintJob();
        try {
            job.print(doc, aset);
        } catch (PrintException e) {
            e.printStackTrace();
        }

        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
